var layer = null;
var form = null;
layui.use(['form', 'layer'], function () {
    form = layui.form;
    layer = layui.layer;
    //监听提交
    var select = '';
    axios.get(ctx + 'sys/menu/select').then(function (res) {
        res.data.menuList.forEach(function (item) {
            select += '<option value="'+ item.menuId + '">' + item.name + '</option>';
        });
        $('#top-menu').html(select);
        form.render();
    });

    form.on('submit(add)', function (data) {
        add(data.field);
        return false;
    });
    form.on('radio(type)', function(data){
        if(data.value == 0) {
            $('.wrapper').empty();
            $('.wrapper').html(
                 '<div class="layui-form-item">'
                +   '<label class="layui-form-label">*目录名称</label>'
                +   '<div class="layui-input-block">'
                +       '<input type="text" name="name" autocomplete="off" placeholder="目录名称" class="layui-input">'
                +   '</div>'
                + '</div>'
                +'<div class="layui-form-item">'
                +   '<label class="layui-form-label">*上级目录</label>'
                +   '<div class="layui-input-block">'
                +       '<select name="parentId" id="top-menu" lay-filter="top-menu">'
                +           select
                +       '</select>'
                +   '</div>'
                +'</div>'
            )
        }else if(data.value == 1) {
            $('.wrapper').empty();
            $('.wrapper').html(
                '<div class="layui-form-item">'
                +   '<label class="layui-form-label">*菜单名称</label>'
                +   '<div class="layui-input-block">'
                +       '<input type="text" name="name" autocomplete="off" placeholder="菜单名称" class="layui-input">'
                +   '</div>'
                +'</div>'
                +'<div class="layui-form-item">'
                +   '<label class="layui-form-label">*上级目录</label>'
                +   '<div class="layui-input-block">'
                +       '<select name="parentId" id="top-menu" lay-filter="top-menu">'
                +           select
                +       '</select>'
                +   '</div>'
                +'</div>'
                +'<div class="layui-form-item">'
                +   '<label class="layui-form-label">*路径</label>'
                +   '<div class="layui-input-block">'
                +       '<input type="text" name="url" autocomplete="off" placeholder="菜单路由" class="layui-input">'
                +   '</div>'
                +'</div>'
            )
        }else if(data.value == 2) {
            $('.wrapper').empty();
            $('.wrapper').html(
                '<div class="layui-form-item">'
                +   '<label class="layui-form-label">*按钮名称</label>'
                +   '<div class="layui-input-block">'
                +       '<input type="text" name="name" autocomplete="off" placeholder="按钮名称" class="layui-input">'
                +   '</div>'
                +'</div>'
                +'<div class="layui-form-item">'
                +   '<label class="layui-form-label">*上级目录</label>'
                +   '<div class="layui-input-block">'
                +       '<select name="parentId" id="top-menu" lay-filter="top-menu">'
                +           select
                +       '</select>'
                +   '</div>'
                +'</div>'
                +'<div class="layui-form-item">'
                +   '<label class="layui-form-label">*授权标识</label>'
                +   '<div class="layui-input-block">'
                +       '<input type="text" name="perms" autocomplete="off" placeholder="授权标识" class="layui-input">'
                +   '</div>'
                +'</div>'
            )
        }
        form.render();
    });
});
$('#cancel').click(function () {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index);
});


function add(data) {
        axios.post(ctx + 'sys/menu/save', data)
            .then(function (res) {
                if (res.data.code == 500) {
                    layer.msg(res.data.msg);
                } else {
                    layer.msg('添加成功');
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                }
            }).catch(function (err) {
            console.log(err)
        });
}